from typing import List


class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        results = []
        nums = sorted(nums)
        nums_len = len(nums)

        for i in range(0, nums_len):
            if nums[i] > 0:
                break 

            if i > 0 and nums[i] == nums[i - 1]:
                continue
            
            m = {}
            viewed = {}
            for j in range(i+1, nums_len):
                if nums[j] in viewed:
                    continue

                if nums[j] in m:
                    results.append([nums[i], m[nums[j]],nums[j]])
                    viewed[nums[j]] = True
                else:
                    n = 0 - nums[i] - nums[j]
                    m[n] = nums[j]

        return results


if __name__ == "__main__":
    nums = [-1, 0, 1, 2, -1, -4, 5, -5, 1, 0, 5, 9]
    print(sorted(nums))
    result = Solution().threeSum(nums)
    print(result)

    nums = [0,0,0]
    result = Solution().threeSum(nums)
    print(result)


    nums = [1,1,-2]
    result = Solution().threeSum(nums)
    print(result)
